﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace FallenValkyrie
{
    public class Animation
    {
        int index, setStartFrame, setEndFrame;

        Texture2D texture;

        public int FrameHeight
        {
            get;
            private set;
        }

        public int FrameWidth
        {
            get;
            private set;
        }

        int framesPerRow;

        public int NumberOfFrames
        {
            get;
            private set;
        }

        int fps;
        public string FPSDisplay
        {
            get;
            private set;
        }

        float timer;
        
        float delayTimer, maxDelay;

        bool repeating, delayed, frameStart;

        /// <summary>
        /// Generates a new Animation Object
        /// </summary>
        /// <param name="content">Content object so we can call .Load</param>
        /// <param name="asset">The asset name of the texture/sprite sheet</param>
        /// <param name="frameWidth">The width of a single frame</param>
        /// <param name="frameHeight">The height of a single frame</param>
        /// 
        /// <param name="numberOfFrames">The total number of frames in this animation</param>
        /// <param name="framesPerSecond">The speed you want the animation to last</param>
        public Animation(ContentManager content, string asset, int frameWidth, int frameHeight, int numberOfFrames, int framesPerSecond)
        {
            texture = content.Load<Texture2D>(asset);
            FrameWidth = frameWidth;
            FrameHeight = frameHeight;
            framesPerRow = texture.Width / frameWidth;
            fps = framesPerSecond;
            FPSDisplay = asset + " FPS:  " + fps.ToString();
            NumberOfFrames = numberOfFrames;
            repeating = false;
            delayed = false;
            frameStart = false;
        }

        public void EnableRepeating()
        {
            repeating = true;
        }

        public void DisableRepeating()
        {
            repeating = false;
        }

        public void SetRepeatingDelay(float maxTime)
        {
            maxDelay = maxTime;
        }

        public void SetStartingFrame(int startNumber)
        {
            setStartFrame = startNumber - 1;
            index = setStartFrame;
            frameStart = true;
        }

        public void SetEndingFrame(int endNumber)
        {
            setEndFrame = endNumber;
            NumberOfFrames = setEndFrame;
        }

        public void Update(GameTime gameTime)
        {
            if (!delayed)
            {
                if (timer > (1.0f / (float)fps))
                {
                    if (index != (NumberOfFrames - 1))
                        index++;
                    else if (repeating)
                    {
                        if (maxDelay > 0)
                        {
                            delayed = true;
                            delayTimer = 0;
                        }
                        else if (!frameStart)
                        {
                            index = 0;
                            EndAnimation = true;
                        }
                        else if (frameStart)
                        {
                            index = setStartFrame;
                        }
                    }

                    timer = 0;

                }
                else
                {
                    timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
                }
            }
            else
            {
                delayTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (delayTimer >= maxDelay)
                {
                    delayed = false;
                    index = 0;
                }
            }

            //if (index == 4)
                //EndAnimation = true;
        }

        public bool EndAnimation;

        public void RestartAnimation()
        {
            index = 0;
            delayed = false;
        }

        /*
        public bool EndAnimation() {
            if (index == NumberOfFrames)
                return true;
            else
                return false;
        }*/
        public Rectangle rect;

        public void Draw(SpriteBatch spriteBatch, Vector2 position)
        {
            int rowNumber = index / framesPerRow;
            rect = new Rectangle((index - (rowNumber * framesPerRow)) * FrameWidth,
                rowNumber * FrameHeight, FrameWidth, FrameHeight);
            spriteBatch.Draw(texture, position, rect, Color.White);
        }

        public void Draw(SpriteBatch spriteBatch, Vector2 position, SpriteEffects spriteEffects, float rotation, float scale)
        {
            int rowNumber = index / framesPerRow;
            spriteBatch.Draw(texture, position, new Rectangle((index - (rowNumber * framesPerRow)) * FrameWidth,
                rowNumber * FrameHeight, FrameWidth, FrameHeight), Color.White, rotation, 
                Vector2.Zero, scale, spriteEffects, 1.0f);
        }
    }
}
